package com.espoir.shp.read01;

import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class CommonShapeReader {
    public ArrayList<CommonShapeModel> readShapeFile(String filePath) {
        ArrayList<CommonShapeModel> modelList = new ArrayList<>();
        File folder = new File(filePath);
        if (!folder.isDirectory()) {
            if (folder.toString().endsWith(".shp")) {
                try {
                    modelList = getShapeFile(folder);
                    return modelList;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("选择的文件后缀名不是.shp");
            }
        } else {
            File[] files = folder.listFiles();
            if (!(files.length > 0)) {
                System.out.println("目录文件为空");
                return modelList;
            }

            for (File file : files) {
                if (!file.toString().endsWith(".shp")) {
                    continue;
                }
                try {
                    ArrayList<CommonShapeModel> models = getShapeFile(file);
                    modelList.addAll(models);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return modelList;
    }

    private ArrayList<CommonShapeModel> getShapeFile(File file) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("url", file.toURI().toURL());
        ArrayList<CommonShapeModel> models = new ArrayList<>();
        DataStore dataStore = DataStoreFinder.getDataStore(map);
        //字符转码，防止中文乱码
        //((ShapefileDataStore) dataStore).setCharset(Charset.forName("utf8"));
        ((ShapefileDataStore) dataStore).setCharset(Charset.forName("gbk"));
        String typeName = dataStore.getTypeNames()[0];

        // source 代表一个 shapefile
        FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);
        //
        FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures();
        // features 代表一行记录
        FeatureIterator<SimpleFeature> features = collection.features();
        while (features.hasNext()) {
            System.out.println("features = " + features);
            SimpleFeature feature = features.next();
            CommonShapeModel model = new CommonShapeModel();
            Iterator<? extends Property> iterator = feature.getValue().iterator();
            while (iterator.hasNext()) {
                Property property = iterator.next();
                //property数据与实体类对应
                if (property.getName().toString().equals("NAME")) {
                    System.out.println("property = " + property.getValue());
                    model.setName(property.getValue().toString());
                }

                if (property.getName().toString().equals("the_geom")){
                    model.setShape(property.getValue().toString());
                    System.out.println("the_geom = " + property.getValue());
                }

            }
            models.add(model);
        }
        return models;
    }
}
